package com.bawei.controller;

import com.bawei.pojo.Menu;
import com.bawei.pojo.UserInfo;
import com.bawei.service.UserInfoService;
import com.bawei.util.PageView;
import com.bawei.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("login")
public class UserLogin {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/checkLogin")
    @ResponseBody
    public ResultEntity checkLogin(String account, String password,HttpSession session){
        ResultEntity res= userInfoService.userInfoService(account,password);
        if(res.getUserInfo()!=null){
            session.setAttribute("user", res.getUserInfo());
        }
        return res;
    }
    @RequestMapping("/menu")
    @ResponseBody
    public List<Menu> menu(){
        return userInfoService.menu("0");
    }

    @RequestMapping("/userInfo/updatepwd")
    @ResponseBody
    public ResultEntity updatePwd(HttpSession session,String oldpwd, String newpwd){
        UserInfo userInfo= (UserInfo) session.getAttribute("user");
        ResultEntity res= userInfoService.updatePwd(userInfo,oldpwd,newpwd);
        if(res.getUserInfo()!=null){
            session.setAttribute("user", res.getUserInfo());
        }
        return res;
    }
    //退出登录
    @RequestMapping("/exit")
    public String exit(HttpSession session){
        session.removeAttribute("user");
        session.invalidate();
        return "redirect:/login.jsp";
    }
}
